﻿using System.Collections.Generic;
using System.Linq;
using ClientOverflow.ViewModels.Interfaces;
using ClientOverflow.ViewModels.MainMenus;

namespace ClientOverflow.ViewModels.QuestionsListViewModels
{
    /// <summary>
    /// QuestionsListViewModel is a view-model that wraps the collection of all questions in the system
    /// and present them to the user in a detailed list. This view-model collects all the questions from
    /// the DB received through the WCF service, wraps each question with its own view-model
    /// (QuestionViewModel) and expose them through the AllQuestions property. The AllQuestions property
    /// is bounded in the UI as a readonly collection that present the question list.
    /// </summary>
    public class QuestionsListViewModel : IQuestionsListViewModel
    {
        public QuestionsListViewModel(IMenusViewModel viewModel, IQuestionsFilter questionsFilter)
        {
            using (var serviceClient = ServicesFactory.CreateClientOverflowServiceClient())
            {
                var questions = serviceClient
                    .GetAllQuestions()
                    .Where(questionsFilter.IsFittingCrieteria);

                foreach (var question in questions)
                {
                    var containintQuestion = question;
                    question.Answers.ForEach(answer => answer.Question = containintQuestion);
                }

                AllQuestions = questions
                    .Select(question => QuestionViewModel.CreateQuestionViewModel(question, viewModel))
                    .ToList();
            }
        }

        public IList<IQuestionInListViewModel> AllQuestions { get; private set; }
    }
}